package app.mvp.contract;

import android.content.Context;
import android.widget.Toast;

import java.util.HashMap;

import app.mvp.base_mvp.BasePresenter;
import app.mvp.utils.NetWorkUtil;
import http.CallBack;
import utils.ToastUtils;

/**
 * Created by 刘智强 on 2017/5/3.
 */

public class LoginPresenter extends BasePresenter {

    @Override
    public void getData(final Context context, String... parameters) {
        if (NetWorkUtil.isNetwork(context)) {
            /**
             * 有网络请求数据
             */
//            final LoginView loginView = (LoginView) view;
            HashMap<String, String> parametersMap = new HashMap<>();
            final String name = parameters[0];
            final String password = parameters[1];
            parametersMap.put("telephone", name);
            parametersMap.put("password", password);
            parametersMap.put("devicekey", "111111");
            baseModel.getModelFromNet(context, isGetDataFromCache, "key", parametersMap, new CallBack<LoginModel>() {
                @Override
                public void start() {

                }

                @Override
                public void completed() {

                }

                @Override
                public void error() {

                }

                @Override
                public void fail(String s, String s1, String s2) {
                    ((LoginView) baseView).loginFailed(s1);
                }

                @Override
                public void success(String s, String s1, LoginModel loginModel) {
                    ToastUtils.myToast(context, loginModel.getTelephone());
                    if (isGetDataFromCache) {
                        baseModel.setCache(context, "key", loginModel);
                    }

                    ((LoginView) baseView).loginSuccess(loginModel);
                }
            });
        } else {
            /**
             * 无网络 加载缓存数据
             */
            LoginModel model = (LoginModel) baseModel.getModelFromCache(context, "key");
            if (model != null) {
                Toast.makeText(context, model.getTelephone(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
